/* Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
 */


#include "stdafx.h"
#include "../通用插座.h"
#include "list物理物体插座.h"


#include "list物体插座.h"
#include "list矢量插座.h"


#include <场景/场景.h>
#include <物理/物理.h>
#include <ppl.h>



using namespace std;





static std::vector<S_物理体*> g_物理体回收站;











C_物理材质_1D插座::C_物理材质_1D插座(wstring name) :C_插座基类(name, E_值类型::e_插座Type_物理材质_Array1D) {
	auto scene = f_scene_get场景(0);
	if (!scene) {
		m_默认数据 = 0;
		return;
	}
	m_默认数据 = f_创建材质(&(scene->m_Physics), 0.5, 0.5, 0.5);

	if (m_默认数据) {
		m_Value.resize(1);
		m_Value[0] = m_默认数据;
	}
}

void* C_物理材质_1D插座::f_getData(uint8 链接ID) {
	C_插座基类* socket = f_getLinkOutSocket(链接ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_插座Type_物理材质_Array1D) {
			return socket->f_getData(0);
		}
		else {
			m_Value.resize(1);
			m_Value[0] = m_默认数据;
		}
	}

	if (m_Value.size() == 0) {
		auto scene = f_scene_get场景(0);
		m_Value.resize(1);
		m_Value[0] = f_创建材质(&(scene->m_Physics), 0.5, 0.5, 0.5, m_物理引擎);
	}

	return &m_Value;
}

void C_物理材质_1D插座::f_setData(void* data, uint8 线程ID) {
	m_Value = *((std::vector<S_物理材质*>*)data);
}








C_物理几何插座_1D::C_物理几何插座_1D(wstring name) :C_插座基类(name, E_值类型::e_插座Type_物理几何_Array1D) {
	
}

void* C_物理几何插座_1D::f_getData(uint8 链接ID) {
	C_插座基类* socket = f_getLinkOutSocket(链接ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_插座Type_物理几何_Array1D) {
			return socket->f_getData(0);
		}
		else {
			auto scene = f_scene_get场景(0);
			if (scene) {
				
			}
		}
	}
	return &m_Value;
}

void C_物理几何插座_1D::f_setData(void* data, int64 ID) {
	m_Value = *((vector<S_物理几何体*>*)data);
}









C_插座_物理体Array1D::C_插座_物理体Array1D(wstring name) :C_插座基类(name, E_值类型::e_插座Type_物理体_Array1D) {
	f_set多输入();
}

C_插座_物理体Array1D::~C_插座_物理体Array1D() {
	for (auto& ob : m_Value) {
		f_Phy_销毁物理体(ob);
		delete ob;
	}
	m_Value.clear();
}

void* C_插座_物理体Array1D::f_getData(uint8 当前插座连接线ID) {
	C_插座基类* socket = f_getLinkOutSocket(当前插座连接线ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_插座Type_物理体_Array1D) {
			return dynamic_cast<C_插座_物理体Array1D*>(socket)->f_getData(当前插座连接线ID);
		}
		else if (socket->m_Type == E_值类型::e_插座Type_引用数据) {
		}
		else if (socket->m_Type == E_值类型::e_插座Type_引用数据) {
			auto& data = *DEF_引用插座数据(socket, 当前插座连接线ID);
			if (data.m_Type == E_值类型::e_Type_Object) {
				m_Value.resize(1);
				if (m_Value[0] == nullptr) m_Value[0] = new S_物理体();
				m_Value[0]->m_绑定物体 = DEF_引用转物体(data);
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_Object_Array1D) {
			auto& data = *DEF_物体插座_Array1D数据(socket, 0);

			int32 num = m_Value.size() - data.size();
			if (num > 0) {
				//重组前多出数据
				for (uint32 i = 0; i < num; ++i) {
					g_物理体回收站.push_back(m_Value.back());
					m_Value.pop_back();
				}
			}
			else if (num < 0) {
				//当前重组需要取回回收的数据
				num = DEF_Min(abs(num), g_物理体回收站.size());
				for (uint32 i = 0; i < num; ++i) {
					m_Value.push_back(g_物理体回收站.back());
					g_物理体回收站.pop_back();
				}

				num = data.size() - m_Value.size();
				if (num > 0) {
					for (uint32 i = 0; i < num; ++i) {
						auto* 物理体 = new S_物理体();
						m_Value.push_back(物理体);
					}
				}
			}

			num = data.size();
			for (uint32 i = 0; i < num; ++i) {
				m_Value[i]->m_绑定物体 = data[i];
			}

			/*m_Value.clear();
			m_Value.reserve(data.size());
			for (auto& ob : data) {
				auto* 物理体 = new S_物理体();
				物理体->m_绑定物体 = ob;
				m_Value.push_back(物理体);
			}*/
		}
		else if (socket->m_Type == E_值类型::e_Type_Object) {
			auto* data = DEF_物体插座数据(socket, 当前插座连接线ID);
			m_Value.resize(1);
			if(m_Value[0] == nullptr) m_Value[0] = new S_物理体();
			m_Value[0]->m_绑定物体 = data;
		}
	}
	else {
		if (m_是否为输出插座 == false) {
			m_Value.clear();
		}
	}
	return &m_Value;
}

void C_插座_物理体Array1D::f_setData(void* data, uint8 线程ID) {
	if (data) {
		m_Value = *((vector<S_物理体*>*)data);
	}
	else {
		m_Value.clear();
	}
	
}

PyObject* C_插座_物理体Array1D::f_bl_传入数据(PyObject* bl_data) {
	return nullptr;
}

PyObject* C_插座_物理体Array1D::f_输出_bl数据(PyObject* bl_data) {
	return nullptr;
}

void C_插座_物理体Array1D::f_释放数据() {
	m_Value.clear();
}




















C_车轮属性插座Array1D::C_车轮属性插座Array1D(wstring name) :C_插座基类(name, E_值类型::e_插座Type_车轮_Array1D) {

}

C_车轮属性插座Array1D::~C_车轮属性插座Array1D() {
}


void* C_车轮属性插座Array1D::f_getData() {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_插座Type_车轮_Array1D) {
			return DEF_车轮属性Array1D插座Value(socket, 0);
		}
	}
	return &m_Value;
}

void C_车轮属性插座Array1D::f_setData(void * data, int64 ID) {
	m_Value = *(vector<S_车轮参数>*)data;
}

PyObject* C_车轮属性插座Array1D::f_输出_bl数据(PyObject* bl_data) {
	auto& data = *DEF_车轮属性Array1D插座Value(this);

	uint32 num = uint32(data.size());
	//data.m_车辆物理体;
	PyObject* r = PyList_New(num+1);
	if (bl_data != (PyObject*)0xffffffff) {

	}
	else {
		//S_Px车轮* body = dynamic_cast<S_Px车轮*>(data.m_车轮物理体);

		//PyList_SetItem(r, 0, f_string转PyObject("车轮组物理配置" + f_整数值转宽字符串(num)));

		for (uint32 i = 0; i < num; ++i) {
			//PyList_SetItem(r, i+1, Py_BuildValue("i", data[i].m_车轮物理体->f_get几何体物理对象()));
		}
		
		/*PyList_SetItem(r, 2, Py_BuildValue("fff", body->m_属性.m_半径, body->m_属性.m_精度, body->m_属性.m_轮胎宽度));

		auto 属性 = Py_BuildValue("fff",
										body->m_属性.m_惯性力矩,
										body->m_属性.m_静止状态外倾角,
										body->m_属性.m_重量,
			body->m_属性.m_最大下垂外倾角,
			body->m_属性.m_最大压缩外倾角,
			body->m_属性.m_最大转向角度,
			body->m_属性.m_手刹强度
		);
		PyList_SetItem(r, 3, 属性);*/
	}

	return r;
}














C_插座车辆属性_1D::C_插座车辆属性_1D(wstring name) :C_插座基类(name, E_值类型::e_插座Type_车辆_Array1D)
{
	//m_车体属性.type = E_值类型::e_插座Type_车辆_Array1D;
	m_Value.resize(1);
}

C_插座车辆属性_1D::~C_插座车辆属性_1D()
{
}

void C_插座车辆属性_1D::f_update(C_插座基类 * 连接到的输入节点, C_节点基类 * node)
{
}

void * C_插座车辆属性_1D::f_getData() {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_插座Type_车辆_Array1D) {
			return dynamic_cast<C_插座车辆属性_1D*>(socket)->f_getData();
		}
		else if (socket->m_Type == E_值类型::e_Type_车辆) {
			//S_车体属性 data = DEF_车辆属性插座Value(socket, 0);
			//m_Value[0] = data;
		}
	}

	return &m_Value;
}

void C_插座车辆属性_1D::f_setData(void* data, int64 ID)
{
	C_插座基类::f_setData(data);

	m_Value = *((vector<S_车体属性>*)data);
	
}










